package com.alliky.home.ui.viewmodel

import android.content.Context
import androidx.lifecycle.viewModelScope
import androidx.paging.ExperimentalPagingApi
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.alliky.core.base.BaseViewModel
import com.rrc.common.entity.BannerVO
import com.rrc.common.entity.CategoryVO
import com.rrc.common.entity.GoodsVO
import com.alliky.core.net.StateLiveData
import com.alliky.home.repo.HomeRepo
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/11/18 10:04
 * ================================================
 */
@ExperimentalPagingApi
class HomeViewModel(private val repo: HomeRepo) : BaseViewModel() {

    val bannerLiveData = StateLiveData<List<BannerVO>>()

    fun getBanner(type: String, context: Context) {
        viewModelScope.launch {
            repo.getBanner(type, bannerLiveData, context)
        }
    }

    fun hotGoodsPagingFlow(): Flow<PagingData<GoodsVO>> =
        repo.getHotGoods().cachedIn(viewModelScope)

    val categoryListData = StateLiveData<List<CategoryVO>>()

    fun getCategory(hashMap: HashMap<String, String>) {
        viewModelScope.launch {
            repo.getCategoryData(hashMap, categoryListData)
        }
    }
}